package SimilarAlgorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import ParticiplePkg.IkParticiple;

public class TestMain {

	public static void main(String[] args) {
		   List<Book> list=new ArrayList<Book>();
		   Book book1=new Book();
		   book1.setBookName("C语言程序设计");
		   book1.setAuthor("谭浩强");
		   book1.setContent("放几点了放几点了金佛大幅降低了房价带来房价大幅");
		   list.add(book1);
		   
		   Book book2=new Book();
		   book2.setBookName("张正华java程序设计");
		   book2.setAuthor("张正华");
		   book2.setContent("封疆大吏房价带何首乌来房价带来房价带来房价的");
		   list.add(book2);
		   
		   Book book3=new Book();
		   book3.setBookName("web智能探究");
		   book3.setAuthor("阿华");
		   book3.setContent("fdf感冒解毒胶囊d中华人民共和国政治局fd");
		   list.add(book3);
		   
		   Book book4=new Book();
		   book4.setBookName("打酱油的程序员");
		   book4.setAuthor("阿华");
		   book4.setContent("我是内容周杰伦毛泽东腾讯");
		   list.add(book4);
		   
		   BookSimiarity bs=new BookSimiarity(new TextSimiarity(new IkParticiple()));
		   //double[][] simiarys= new double[list.size()][list.size()];
		   
		   for(int i=0;i<list.size();i++){
			   Book b1=list.get(i);
			   for(int j=0;j<list.size();j++){
				   if(i!=j){
					   Book b2=list.get(j);
					   b1.getSims().put(b2,bs.getSimilarity(b1,b2));   
				   }
				   
			   }   
			}
		   
		   for(int i=0;i<list.size();i++){
			  Book bk=list.get(i);
			  Entry<Book,Double>[] sim=orderBy(bk.getSims());
			  System.out.println("========"+bk.getBookName()+"=======");
			  for(Entry<Book,Double> ent:sim){
				  Book bk0=ent.getKey();
				  System.out.println("------bookname: "+bk0.getBookName()+"------");
				  System.out.println("author: "+bk0.getAuthor());
				  System.out.println("similary: "+ent.getValue());
			  }
		   }
		 
           
	}
	
	private static Entry<Book,Double>[] orderBy(Map<Book,Double> sims){
		Set<Entry<Book,Double>> set=sims.entrySet();
		Map.Entry<Book,Double>[] entries=(Map.Entry[])set.toArray(new Map.Entry[sims.size()]); 
		Arrays.sort(entries,new Comparator<Entry<Book,Double>>(){

			public int compare(Entry<Book, Double> o1,
					Entry<Book, Double> o2) {
				int returnInt=((o1.getValue()-o2.getValue())>0)?-1:1;
				return returnInt;
			}
			

		});
		
		return entries;
	}

}
